<?php

namespace App\Services;

use App\Exceptions\GeneralException;
use App\Models\User;

class UserService
{

    /**
     * Description:创建用户
     * @param array $params
     * @return User
     * @throws GeneralException
     */
    public function store(array $params)
    {
    }



    public function adminIndexUser(array $params)
    {
        $page = !empty($params['page']) ? $params['page'] : 1;
        $limit = !empty($params['limit']) ? $params['limit'] : 10;
        $status = !empty($params['status']) ? $params['status'] : null;
        $mobile = !empty($params['phone']) ? $params['phone'] : null;
        $id = !empty($params['id']) ? $params['id'] : null;
        $begin = !empty($params['begin']) ? $params['begin'] : null;
        $end = !empty($params['end']) ? $params['end'] : null;
        $query = User::query();
        if ($begin) {
            $query->where('created_at', '>=', $params['begin']);
        }
        if ($end) {
            $query->where('created_at', '>=', $params['end']);
        }
        if ($id) {
            $query->where('id', $id);
        }
        if ($mobile) {
            $query->where('phone', 'like', "%{$mobile}%");
        }
        if ($status) {
            $query->where('status', $status);
        }

        $list = $query->orderByDesc('created_at')->paginate($limit, ['*'], 'page', $page);
        return $list;
    }

    public function showUser(int $id)
    {
        return User::query()->findOrFail($id);
    }

    public function updateUser(array $params)
    {
        $user = User::query()->find($params['id']);
        $user->fill($params);
        $user->save();
        return true;
    }
}
